package com.ruoyi.web.controller.module.property;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.property.PropertyFeeOrderTemplate;
import com.ruoyi.system.domain.module.property.PropertyFeeOrderTemplateApprover;
import com.ruoyi.system.domain.module.property.PropertyFeeOrderTemplateApproverInfo;
import com.ruoyi.system.service.module.property.IPropertyFeeOrderTemplateApproverInfoService;
import com.ruoyi.system.service.module.property.IPropertyFeeOrderTemplateApproverService;
import com.ruoyi.system.service.module.property.IPropertyFeeOrderTemplateService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 物业费相关费用模板的修改审核Controller
 *
 * @author ruoyi
 * @date 2025-09-27
 */
@Validated
@Api(value = "物业费相关费用模板的修改审核控制器", tags = {"物业费相关费用模板的修改审核管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/templateApprover")
public class PropertyFeeOrderTemplateApproverController extends BaseController {

    private final IPropertyFeeOrderTemplateApproverService propertyFeeOrderTemplateApproverService;
	private final IPropertyFeeOrderTemplateApproverInfoService propertyFeeOrderTemplateApproverInfoService;
	private final IPropertyFeeOrderTemplateService propertyFeeOrderTemplateService;

    /**
     * 查询物业费相关费用模板的修改审核列表
     */
    @ApiOperation("查询物业费相关费用模板的修改审核列表")
    @PreAuthorize("@ss.hasPermi('module:templateApprover:list')")
    @GetMapping("/list")
    public TableDataInfo<PropertyFeeOrderTemplateApprover> list(@Validated PropertyFeeOrderTemplateApprover propertyFeeOrderTemplateApprover) {
        QueryWrapper<PropertyFeeOrderTemplateApprover> qw = new QueryWrapper<>();
		if (propertyFeeOrderTemplateApprover.getApproverName()!=null){
			qw.like("approver_name",propertyFeeOrderTemplateApprover.getApproverName());
		}
		if (propertyFeeOrderTemplateApprover.getApplicantName()!=null){
			qw.like("applicant_name",propertyFeeOrderTemplateApprover.getApplicantName());
		}
		if (propertyFeeOrderTemplateApprover.getBeginCreateTime()!=null&&propertyFeeOrderTemplateApprover.getEndCreateTime()!=null){
			qw.between("create_time",propertyFeeOrderTemplateApprover.getBeginCreateTime(),propertyFeeOrderTemplateApprover.getEndCreateTime());
		}
		if (propertyFeeOrderTemplateApprover.getBeginApproverTime()!=null&&propertyFeeOrderTemplateApprover.getEndApproverTime()!=null){
			qw.between("approver_time",propertyFeeOrderTemplateApprover.getBeginApproverTime(),propertyFeeOrderTemplateApprover.getEndApproverTime());
		}
		SysUser sysUser = getUser();
		if (sysUser.getEstateId()!=null){
			qw.eq("estate_id",sysUser.getEstateId());
		}else {
			if (propertyFeeOrderTemplateApprover.getEstateId()!=null){
				qw.eq("estate_id",propertyFeeOrderTemplateApprover.getEstateId());
			}
		}
		qw.orderByDesc("create_time");
        TableDataInfo<PropertyFeeOrderTemplateApprover> td = PageUtils.buildDataInfo(propertyFeeOrderTemplateApproverService.page(PageUtils.buildPage(),qw));
        List<PropertyFeeOrderTemplateApprover> list = td.getRows();
		if (list.size()>0){
			for (PropertyFeeOrderTemplateApprover data:list){
				String Id = String.valueOf(data.getId());
				for (int i=Id.length();i<=6;i++){
					Id = "0"+Id;
				}
				data.setApproverNo("SPD"+Id);
			}
		}
        return td;
    }

    /**
     * 导出物业费相关费用模板的修改审核列表
     */
    @ApiOperation("导出物业费相关费用模板的修改审核列表")
    @PreAuthorize("@ss.hasPermi('module:templateApprover:export')")
    @Log(title = "物业费相关费用模板的修改审核", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<PropertyFeeOrderTemplateApprover> export(@Validated PropertyFeeOrderTemplateApprover propertyFeeOrderTemplateApprover) {
        List<PropertyFeeOrderTemplateApprover> list = propertyFeeOrderTemplateApproverService.list();
        ExcelUtil<PropertyFeeOrderTemplateApprover> util = new ExcelUtil<PropertyFeeOrderTemplateApprover>(PropertyFeeOrderTemplateApprover.class);
        return util.exportExcel(list, "物业费相关费用模板的修改审核");
    }

    /**
     * 获取物业费相关费用模板的修改审核详细信息
     */
    @ApiOperation("获取物业费相关费用模板的修改审核详细信息")
    @PreAuthorize("@ss.hasPermi('module:templateApprover:query')")
    @GetMapping("/{id}")
    public AjaxResult<PropertyFeeOrderTemplateApprover> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        PropertyFeeOrderTemplateApprover propertyFeeOrderTemplateApprover = propertyFeeOrderTemplateApproverService.getById(id);
        return AjaxResult.success(propertyFeeOrderTemplateApprover);
    }

    /**
     * 新增物业费相关费用模板的修改审核
     */
    @ApiOperation("新增物业费相关费用模板的修改审核")
    @PreAuthorize("@ss.hasPermi('module:templateApprover:add')")
    @Log(title = "物业费相关费用模板的修改审核", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody PropertyFeeOrderTemplateApprover propertyFeeOrderTemplateApprover) {
        return toAjax(propertyFeeOrderTemplateApproverService.save(propertyFeeOrderTemplateApprover) ? 1 : 0);
    }

    /**
     * 修改物业费相关费用模板的修改审核
     */
    @ApiOperation("修改物业费相关费用模板的修改审核")
    @PreAuthorize("@ss.hasPermi('module:templateApprover:edit')")
    @Log(title = "物业费相关费用模板的修改审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody PropertyFeeOrderTemplateApprover propertyFeeOrderTemplateApprover) {
		//判断审批是否通过
		if (propertyFeeOrderTemplateApprover.getState() == 1) {
			//根据审批记录查到修改记录
			QueryWrapper<PropertyFeeOrderTemplateApproverInfo> qw = new QueryWrapper<>();
			qw.eq("approver_id",propertyFeeOrderTemplateApprover.getId());
			List<PropertyFeeOrderTemplateApproverInfo> infoList = propertyFeeOrderTemplateApproverInfoService.list(qw);
			if (infoList.size()>0){
				for (PropertyFeeOrderTemplateApproverInfo info:infoList){
					//根据info修改模板记录
					PropertyFeeOrderTemplate template = propertyFeeOrderTemplateService.findById(info.getTemplateId());
					if (template!=null){
						template.setYouhuiPrice(propertyFeeOrderTemplateApprover.getTotalPrice());
						propertyFeeOrderTemplateService.updateById(template);

					}
				}
			}
		}
		//添加审核时间 审核人
		SysUser user = getUser();
		Date date = new Date();
		propertyFeeOrderTemplateApprover.setApproverTime(date);
		propertyFeeOrderTemplateApprover.setApproverName(user.getNickName());
		propertyFeeOrderTemplateApprover.setApproverId(user.getUserId());
		return toAjax(propertyFeeOrderTemplateApproverService.updateById(propertyFeeOrderTemplateApprover) ? 1 : 0);

    }

    /**
     * 删除物业费相关费用模板的修改审核
     */
    @ApiOperation("删除物业费相关费用模板的修改审核")
    @PreAuthorize("@ss.hasPermi('module:templateApprover:remove')")
    @Log(title = "物业费相关费用模板的修改审核" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(propertyFeeOrderTemplateApproverService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
